/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package by.bsuir.bbs.persistence;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Администратор
 */
@Entity
@Table(name = "ad")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Ad.findAll", query = "SELECT a FROM Ad a"),
    @NamedQuery(name = "Ad.findByAdId", query = "SELECT a FROM Ad a WHERE a.adPK.adId = :adId"),
    @NamedQuery(name = "Ad.findByTitle", query = "SELECT a FROM Ad a WHERE a.title = :title"),
    @NamedQuery(name = "Ad.findByDescription", query = "SELECT a FROM Ad a WHERE a.description = :description"),
    @NamedQuery(name = "Ad.findByCreationTime", query = "SELECT a FROM Ad a WHERE a.creationTime = :creationTime"),
    @NamedQuery(name = "Ad.findByAvailableTo", query = "SELECT a FROM Ad a WHERE a.availableTo = :availableTo"),
    @NamedQuery(name = "Ad.findByUserIduser", query = "SELECT a FROM Ad a WHERE a.adPK.userIduser = :userIduser")})
public class Ad implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected AdPK adPK;
    @Size(max = 45)
    @Column(name = "title")
    private String title;
    @Size(max = 200)
    @Column(name = "description")
    private String description;
    @Column(name = "creation_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationTime;
    @Column(name = "available_to")
    @Temporal(TemporalType.DATE)
    private Date availableTo;
    @JoinColumn(name = "user_iduser", referencedColumnName = "user_id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User user;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ad")
    private Collection<Comment> commentCollection;

    public Ad() {
    }

    public Ad(AdPK adPK) {
        this.adPK = adPK;
    }

    public Ad(int adId, int userIduser) {
        this.adPK = new AdPK(adId, userIduser);
    }

    public AdPK getAdPK() {
        return adPK;
    }

    public void setAdPK(AdPK adPK) {
        this.adPK = adPK;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    public Date getAvailableTo() {
        return availableTo;
    }

    public void setAvailableTo(Date availableTo) {
        this.availableTo = availableTo;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @XmlTransient
    public Collection<Comment> getCommentCollection() {
        return commentCollection;
    }

    public void setCommentCollection(Collection<Comment> commentCollection) {
        this.commentCollection = commentCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (adPK != null ? adPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Ad)) {
            return false;
        }
        Ad other = (Ad) object;
        if ((this.adPK == null && other.adPK != null) || (this.adPK != null && !this.adPK.equals(other.adPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "by.bsuir.bbs.jpa.Ad[ adPK=" + adPK + " ]";
    }
    
}
